package 剑指offer;

import 抽象数据类型.TreeNode;

import java.util.ArrayList;
import java.util.Arrays;

import static 抽象数据类型.TreeNode.buildTree;

/**
 * @description:
 * @author: ywk
 * @date: 2021-04-07
 */
public class 根节点到所有叶子节点的所有路径和 {

    static int sum = 0;

    public static int sumNumbers(TreeNode<Integer> root) {
        // write code here
        if (root != null) {
            sumNumbers(root, 0);
        }
        return sum;
    }

    public static void sumNumbers(TreeNode<Integer> root, int num) {
        if (root == null) {
            return;
        }
        num = num * 10 + root.val;
        if (root.left == null && root.right == null) {
            sum += num;
            return;
        }
        sumNumbers(root.left, num);
        sumNumbers(root.right, num);

    }

    public static void main(String[] args) {
        TreeNode treeNode =buildTree(new ArrayList<>(Arrays.asList(1, 0, null)));
        System.out.println(sumNumbers(treeNode));
    }
}
